<?php 
$settings = CJSON::decode($template->settings);
$minqties = CJSON::decode($template->min_quantity);
?>
<script type="text/javascript">
    $(window).ready(function(){
        $('.quantity-input').keyup(function(e){
            var value = $(this).val();
            var minqty1 =$(this).parent('td').parent('tr').attr('data-minqty1'); 
            var minqty2 =$(this).parent('td').parent('tr').attr('data-minqty2');
            var price = $(this).data('price');
            if ( value < minqty1){
				
                $(this).parent('td').parent('tr').find('.total').html((price*value).toFixed(2));
				
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
            }else if((minqty1<=value && minqty2 == 0) || (minqty1<=value && value<minqty2)){
				var discount1 = parseFloat($(this).parent('td').parent('tr').attr('data-discount1'));
				var price_dis =price - (price*(discount1/1000));
                $(this).parent('td').parent('tr').find('.total').html((price_dis*value).toFixed(2));

                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount2').parent('td').removeClass('text-danger').addClass('text-muted');
			}else if(minqty2<=value){
				var discount2 = parseFloat($(this).parent('td').parent('tr').attr('data-discount2'));
				var price_dis =price - (price*(discount2/1000));
                $(this).parent('td').parent('tr').find('.total').html((price_dis*value).toFixed(2));
                
                $(this).parent('td').parent('tr').find('.discount2').parent('td').removeClass('text-muted').addClass('text-danger');
                $(this).parent('td').parent('tr').find('.base-price').parent('td').removeClass('text-danger').addClass('text-muted');
                $(this).parent('td').parent('tr').find('.discount1').parent('td').removeClass('text-danger').addClass('text-muted');
			}
            var total = 0;
            $('.total').each(function(index){
                total = total + parseFloat($(this).html());
            });
            $('#final').html(total.toFixed(2));
            $('#final-input').val(total.toFixed(2));
        });

        $('.type-select').on('change', function(){
            if ($(this).val() == 1 || $(this).val() == 2){
                $(this).parent('td').parent('tr').find('type-quantity').prop('disabled', false);
            }else{
                $(this).parent('td').parent('tr').find('type-quantity').prop('disabled', true);
            }
        });
    });
</script>
<?php 
if(isset($category)){
    foreach($category as $key){ 
		if(!empty($settings)){
			$current_discount=array();
			foreach($settings as $setting => $value_setting){
				if($setting == $key['id'])
					$current_discount =$value_setting;
			}
		}
		//var_dump($current_discount);
		if(!empty($minqties)){
			$current_minqty=array();
			foreach($minqties as $minqty => $value_minqty){
				if($minqty == $key['id'])
					$current_minqty =$value_minqty;
			}
		}
		//var_dump($current_minqty);
	?>
        <h3><?php echo $key['title'] ?></h3>
        <table class="table table-striped table-hover" id="order">
                <tr>
                    <th>Photo</th>
                    <th>Title</th>
                    <th class="text-center" style="width: 70px">Promo<br/><small style="font-size: 9px;" class="text-muted">units</small></th>
                    <th class="text-center" style="width: 70px">Pickup<br/><small style="font-size: 9px;" class="text-muted">units</small></th>
                    <th class="text-center" style="width: 70px">Order<br/><small style="font-size: 9px;" class="text-muted">inner qty</small></th>
                    <th>Base Price</th>
                    <th>Discount 1<br/>
						<?php  if(isset($current_minqty[0])){?>
						<span class="minqty-value1" style="font-size: 9px" class="text-muted"><?php echo $current_minqty[0];?></span> = <span class="discount-value1" data-discount="<?php echo $current_discount[0]/10?>" style="font-size: 9px" class="text-muted"><?php echo $current_discount[0]/10?>%</span>
						<?php }else{?>
						<span class="discount-value1" style="font-size: 9px" class="text-muted">0%</span>
						<?php }?>
					</th>
                    <th>Discount 2<br/>
						<?php  if(isset($current_minqty[1])){?>
						<span class="minqty-value2" style="font-size: 9px" class="text-muted"><?php echo $current_minqty[1];?></span> = <span class="discount-value1" data-discount="<?php echo $current_discount[1]/10?>" style="font-size: 9px" class="text-muted"><?php echo $current_discount[1]/10?>%</span>
						<?php }else{?>
						<span class="discount-value2" style="font-size: 9px" class="text-muted">0%</span>
						<?php }?>
					</th>
                    <th class="text-right">Total</th>
                </tr>
                <?php
                    $sql_tmp = 'SELECT * FROM products WHERE  category_id = '. $key['id'].' and id IN ('. implode(", ",$result). ')' ;
                    $product_category = Yii::app()->db->createCommand($sql_tmp)->queryAll();
                    if(isset($product_category)){
                        foreach($product_category as $item){ 
						
							$minqty1 = 0;
							$minqty2 = 0;
							$discount1 = 0;
							$discount2 = 0;
							if(isset($current_minqty[0])) $minqty1 = $current_minqty[0];
							if(isset($current_minqty[1])) $minqty2 = $current_minqty[1];
							if(isset($current_discount[0])) $discount1 = $current_discount[0];
							if(isset($current_discount[1])) $discount2 = $current_discount[1];
						   
                           ?>
                            <tr data-minqty1="<?php echo $minqty1;?>" data-minqty2="<?php echo $minqty2;?>" data-discount1="<?php echo $discount1;?>" data-discount2="<?php echo $discount2;?>">
                                <td><img width="32px" height="32px" src="/public/products/<?php echo $item['photo']?>"/></td>
                                <td><?php echo $item['title']?></td>
                                <td class="text-center"><input value="" name="promo[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center promo-quantity"/></td>
                                <td class="text-center"><input value="" name="pickup[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center pickup-quantity"/></td>
                                <td class="text-center"><input data-price="<?php echo $item['price']?>" value="" name="products[<?php echo $item['id']?>]" style="width:50px" type="text" class="form-control text-center quantity-input"/></td>
                                <td class="text-danger">$<span class="base-price"><?php echo $item['price']?></span></td>
                                <td class="text-muted">
								    <?php if(isset($current_discount[0])) { ?>
									$<span class="discount1"><?php echo round(($item['price'] - ($item['price'] * ($current_discount[0]/1000))),2);?></span>
									<?php }?>
								</td>
                                <td class="text-muted">
									<?php if(isset($current_discount[1])) { ?>
									$<span class="discount2"><?php echo round(($item['price'] - ($item['price'] * ($current_discount[1]/1000))),2);?></span>
									<?php }?>
								</td>
                                <td class="text-right">$<span class="total">0</span></td>
                                 
                            </tr>
                            
                       <?php  } ?> </table>
                   <?php   }
                   
              } 
           }  ?>
            <table class="table table-striped table-hover">
                 <tr>
                 <td style="width: 80%;"> </td>
                    <td class="text-right" style="width: 20%;">
                        <strong>Total: $<span id="final">0</span></strong>
                        <input type="hidden" name="data[final]" id="final-input"/>
                    </td>
                </tr>
            </table>
